package com.swh.spit.controller;

import com.swh.base.entity.Result;
import com.swh.base.entity.StatusCode;
import com.swh.spit.entity.spit;
import com.swh.spit.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author swh
 * @Description tb_pl 控制器层
 * @date 2021-01-13 17:36:42
 */
@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    SpitService spitService;


    /**
     * 分享成功+1
     * thumbup+1
     * @return
     **/
    @RequestMapping(value = "/share/{share}", method = RequestMethod.PUT)
    public Result share(@PathVariable("share") String share){
        spitService.share(share);
        return new Result(StatusCode.OK,"分享成功+1");
    }

    /**
     * 增加浏览量
     * thumbup+1
     * @return
     **/
    @RequestMapping(value = "/views/{v}", method = RequestMethod.PUT)
    public Result views(@PathVariable("v") String v){
        spitService.views(v);
        return new Result(StatusCode.OK,"浏览量+1");
    }


    /**
     * 发布吐槽
     * @return
     **/
    @RequestMapping(value = "/spsit", method = RequestMethod.POST)
    public Result save(spit spsit) {
        spitService.save(spsit);
        return new Result(StatusCode.OK,"发布成功");
    }

    /**
     * 取消吐槽点赞
     * thumbup-1
     * @return
     **/
    @RequestMapping(value = "/teasing/{spitid}", method = RequestMethod.PUT)
    public Result cancelTeasing(@PathVariable("spitid") String spitid) {
        spitService.cancelTeasing(spitid);
        return new Result(StatusCode.OK,"取消吐槽点赞成功");
    }

    /**
     * 吐槽点赞
     * thumbup+1
     * @return
     **/
    @RequestMapping(value = "/thumbup/{spitid}", method = RequestMethod.PUT)
    public Result thumbup(@PathVariable("spitid") String spitid) {
        spitService.thumbup(spitid);
        return new Result(StatusCode.OK,"点赞成功");
    }

        /**
         * 查询全部
         *
         * @return
         **/
        @RequestMapping(method = RequestMethod.GET)
        public Result findAll () {
            List<spit> list = spitService.findAll();
            return new Result(StatusCode.OK, "MongoDB查询全部", list);
        }

        /**
         * 根据id查询
         *
         * @return
         **/
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        public Result findById (@PathVariable("id") String id){
            spit spit = spitService.findById(id);
            return new Result(StatusCode.OK, "查询成功", spit);
        }

        /**
         * 修改
         *
         * @return
         **/
        @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
        public Result update (@PathVariable("id") String id, spit spit){
//        new BasicDBObject()
            spit.setId(id);

            spit.setContent("历史记录");
            spit.setUserid("1");
            spit.setVisits(1000);
            spit.setNickname("呜呜呜");
            spitService.update(spit);
            return new Result(StatusCode.OK, "修改成功");
        }

        /**
         *  删除
         * @return
         **/
        @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
        public Result delete (@PathVariable("id") String id){
            spitService.delete(id);
            return new Result(StatusCode.OK, "删除成功");
        }

        /**
         * 增加
         * @return
         **/
        @RequestMapping(method = RequestMethod.POST)
        public Result insert (spit s){
            spitService.insert(s);
            return new Result(StatusCode.OK, "添加成功");
        }
    }

